home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / httpd / cgi-src / imagemap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-09  |  7.3 KB  |  251 lines

  1. /*
  2. ** mapper 1.2
  3. ** 7/26/93 Kevin Hughes, kevinh@pulua.hcc.hawaii.edu
  4. ** "macmartinized" polygon code copyright 1992 by Eric Haines, erich@eye.com
  5. ** All suggestions, help, etc. gratefully accepted!
  6. **
  7. ** 1.1 : Better formatting, added better polygon code.
  8. ** 1.2 : Changed isname(), added config file specification.
  9. **
  10. ** 11/13/93: Rob McCool, robm@ncsa.uiuc.edu
  11. **
  12. ** Rewrote configuration stuff for NCSA /htbin script
  13. **
  14. ** 12/05/93: Rob McCool, robm@ncsa.uiuc.edu
  15. ** 
  16. ** Made CGI/1.0 compliant.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22.  
  23. #define CONF_FILE "/usr/local/etc/httpd/conf/imagemap.conf"
  24.  
  25. #define MAXLINE 500
  26. #define MAXVERTS 100
  27. #define X 0
  28. #define Y 1
  29.  
  30. int isname(char);
  31.  
  32. int main(int argc, char **argv)
  33. {
  34.     char input[MAXLINE], *mapname, def[MAXLINE], conf[80];
  35.     double testpoint[2], pointarray[MAXVERTS][2];
  36.     int i, j, k;
  37.     FILE *fp;
  38.     char *t;
  39.     
  40.     if (argc != 2)
  41.         servererr("Wrong number of arguments, client may not support ISMAP.");
  42.     mapname=getenv("PATH_INFO");
  43.  
  44.     if((!mapname) || (!mapname[0]))
  45.         servererr("No map name given. Please read the <A HREF=\"http://hoohoo.ncsa.uiuc.edu/docs/setup/admin/Imagemap.html\">instructions</A>.<P>");
  46.  
  47.  
  48.     mapname++;
  49.     if(!(t = strchr(argv[1],',')))
  50.         servererr("Your client doesn't support image mapping properly.");
  51.     *t++ = '\0';
  52.     testpoint[X] = (double) atoi(argv[1]);
  53.     testpoint[Y] = (double) atoi(t);
  54.     
  55.     if ((fp = fopen(CONF_FILE, "r")) == NULL)
  56.         servererr("Couldn't open configuration file.");
  57.  
  58.     while(!(getline(input,MAXLINE,fp))) {
  59.         char confname[MAXLINE];
  60.         if((input[0] == '#') || (!input[0]))
  61.             continue;
  62.         for(i=0;isname(input[i]) && (input[i] != ':');i++)
  63.             confname[i] = input[i];
  64.         confname[i] = '\0';
  65.         if(!strcmp(confname,mapname))
  66.             break;
  67.     }
  68.     if(feof(fp))
  69.         servererr("Map not found in configuration file.");
  70.  
  71.     fclose(fp);
  72.     while(isspace(input[i]) || input[i] == ':') ++i;
  73.  
  74.     for(j=0;input[i] && isname(input[i]);++i,++j)
  75.         conf[j] = input[i];
  76.     conf[j] = '\0';
  77.  
  78.     if(!(fp=fopen(conf,"r")))
  79.         servererr("Couldn't open map file.");
  80.     
  81.     while(!(getline(input,MAXLINE,fp))) {
  82.         char type[MAXLINE];
  83.         char url[MAXLINE];
  84.         char num[10];
  85.  
  86.         if((input[0] == '#') || (!input[0]))
  87.             continue;
  88.  
  89.         type[0] = '\0';url[0] = '\0';
  90.  
  91.         for(i=0;isname(input[i]) && (input[i]);i++)
  92.             type[i] = input[i];
  93.         type[i] = '\0';
  94.  
  95.         while(isspace(input[i])) ++i;
  96.         for(j=0;input[i] && isname(input[i]);++i,++j)
  97.             url[j] = input[i];
  98.         url[j] = '\0';
  99.  
  100.         if(!strcmp(type,"default")) {
  101.             strcpy(def,url);
  102.             continue;
  103.         }
  104.  
  105.         k=0;
  106.         while (input[i]) {
  107.             while (isspace(input[i]) || input[i] == ',')
  108.                 i++;
  109.             j = 0;
  110.             while (isdigit(input[i]))
  111.                 num[j++] = input[i++];
  112.             num[j] = '\0';
  113.             if (num[0] != '\0')
  114.                 pointarray[k][X] = (double) atoi(num);
  115.             else
  116.                 break;
  117.             while (isspace(input[i]) || input[i] == ',')
  118.                 i++;
  119.             j = 0;
  120.             while (isdigit(input[i]))
  121.                 num[j++] = input[i++];
  122.             num[j] = '\0';
  123.             if (num[0] != '\0')
  124.                 pointarray[k++][Y] = (double) atoi(num);
  125.             else {
  126.                 fclose(fp);
  127.                 servererr("Missing y value.");
  128.             }
  129.         }
  130.         pointarray[k][X] = -1;
  131.         if(!strcmp(type,"poly"))
  132.             if(pointinpoly(testpoint,pointarray))
  133.                 sendmesg(url);
  134.         if(!strcmp(type,"circle"))
  135.             if(pointincircle(testpoint,pointarray))
  136.                 sendmesg(url);
  137.         if(!strcmp(type,"rect"))
  138.             if(pointinrect(testpoint,pointarray))
  139.                 sendmesg(url);
  140.     }
  141.     if(def[0])
  142.         sendmesg(def);
  143.     servererr("No default specified.");
  144. }
  145.  
  146. sendmesg(char *url)
  147. {
  148.     printf("Location: %s%c%c",url,10,10);
  149.     printf("This document has moved <A HREF=\"%s\">here</A>%c",url,10);
  150.     exit(1);
  151. }
  152.  
  153. int pointinrect(double point[2], double coords[MAXVERTS][2])
  154. {
  155.         return ((point[X] >= coords[0][X] && point[X] <= coords[1][X]) &&
  156.         (point[Y] >= coords[0][Y] && point[Y] <= coords[1][Y]));
  157. }
  158.  
  159. int pointincircle(double point[2], double coords[MAXVERTS][2])
  160. {
  161.         int radius1, radius2;
  162.  
  163.         radius1 = ((coords[0][Y] - coords[1][Y]) * (coords[0][Y] -
  164.         coords[1][Y])) + ((coords[0][X] - coords[1][X]) * (coords[0][X] -
  165.         coords[1][X]));
  166.         radius2 = ((coords[0][Y] - point[Y]) * (coords[0][Y] - point[Y])) +
  167.         ((coords[0][X] - point[X]) * (coords[0][X] - point[X]));
  168.         return (radius2 <= radius1);
  169. }
  170.  
  171. int pointinpoly(double point[2], double pgon[MAXVERTS][2])
  172. {
  173.         int i, numverts, inside_flag, xflag0;
  174.         int crossings;
  175.         double *p, *stop;
  176.         double tx, ty, y;
  177.  
  178.         for (i = 0; pgon[i][X] != -1 && i < MAXVERTS; i++)
  179.                 ;
  180.         numverts = i;
  181.         crossings = 0;
  182.  
  183.         tx = point[X];
  184.         ty = point[Y];
  185.         y = pgon[numverts - 1][Y];
  186.  
  187.         p = (double *) pgon + 1;
  188.         if ((y >= ty) != (*p >= ty)) {
  189.                 if ((xflag0 = (pgon[numverts - 1][X] >= tx)) ==
  190.                 (*(double *) pgon >= tx)) {
  191.                         if (xflag0)
  192.                                 crossings++;
  193.                 }
  194.                 else {
  195.                         crossings += (pgon[numverts - 1][X] - (y - ty) *
  196.                         (*(double *) pgon - pgon[numverts - 1][X]) /
  197.                         (*p - y)) >= tx;
  198.                 }
  199.         }
  200.  
  201.         stop = pgon[numverts];
  202.  
  203.         for (y = *p, p += 2; p < stop; y = *p, p += 2) {
  204.                 if (y >= ty) {
  205.                         while ((p < stop) && (*p >= ty))
  206.                                 p += 2;
  207.                         if (p >= stop)
  208.                                 break;
  209.                         if ((xflag0 = (*(p - 3) >= tx)) == (*(p - 1) >= tx)) {
  210.                                 if (xflag0)
  211.                                         crossings++;
  212.                         }
  213.                         else {
  214.                                 crossings += (*(p - 3) - (*(p - 2) - ty) *
  215.                                 (*(p - 1) - *(p - 3)) / (*p - *(p - 2))) >= tx;
  216.                         }
  217.                 }
  218.                 else {
  219.                         while ((p < stop) && (*p < ty))
  220.                                 p += 2;
  221.                         if (p >= stop)
  222.                                 break;
  223.                         if ((xflag0 = (*(p - 3) >= tx)) == (*(p - 1) >= tx)) {
  224.                                 if (xflag0)
  225.                                         crossings++;
  226.                         }
  227.                         else {
  228.                                 crossings += (*(p - 3) - (*(p - 2) - ty) *
  229.                                 (*(p - 1) - *(p - 3)) / (*p - *(p - 2))) >= tx;
  230.                         }
  231.                 }
  232.         }
  233.         inside_flag = crossings & 0x01;
  234.         return (inside_flag);
  235. }
  236.  
  237. servererr(char *msg)
  238. {
  239.     printf("Content-type: text/html%c%c",10,10);
  240.     printf("<title>Mapping Server Error</title>");
  241.     printf("<h1>Mapping Server Error</h1>");
  242.     printf("This server encountered an error:<p>");
  243.     printf("%s", msg);
  244.     exit(-1);
  245. }
  246.  
  247. int isname(char c)
  248. {
  249.         return (!isspace(c));
  250. }
  251.